package com.ph.irp.stock.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ph.irp.base.currency.dao.PBaseCurrencyDAO;
import com.ph.irp.base.measure.dao.PBaseMeasureUnitDAO;
import com.ph.irp.base.settlement.dao.PBaseSettlementDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprogectSupplierDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.dto.StockOrderDTO;
import com.ph.irp.dto.StockOrderDetailDTO;
import com.ph.irp.model.PBaseCurrency;
import com.ph.irp.model.PBaseMeasureunit;
import com.ph.irp.model.PBaseSettlement;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectDepartment;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.irp.model.PBaseVerifyprojectSupplier;
import com.ph.irp.model.PStockOrderdetail;
import com.ph.irp.model.PStockOrder;
import com.ph.irp.stock.dao.StockOrderDao;
import com.ph.irp.stock.dao.StockOrderDetailDao;
import com.ph.irp.stock.service.StockOrderService;

public class StockOrderServiceImpl implements StockOrderService {
	//采购订单dao
	private StockOrderDao stockOrderDaoProxy = null;
	//采购订单明细dao
	private StockOrderDetailDao stockOrderDetailDaoProxy = null;
	//员工dao
	private PBaseVerifyprojectCandiateDao candiateDaoProxy = null ;
	//币别dao
	private PBaseCurrencyDAO currencyDaoProxy = null ;
	//部门dao
	private PBaseVerifyprojectDepartmentDao  departmentDaoProxy = null;
	//结算方式dao
	private PBaseSettlementDAO settlementProxy = null ;
	//供应商dao
	private PBaseVerifyprogectSupplierDao supplierDaoProxy = null ;
	//计量单位DAO
	private PBaseMeasureUnitDAO measureUnitDaoProxy = null;
	//物料dao
	private PBaseVerifyprojectMaterialDao materialDaoProxy = null;
	/**
	 * 新增采购订单及明细
	 */
	@Override
	public void addStockOrder(PStockOrder stockOrder,
			List<PStockOrderdetail> stockOrderDetails) throws Throwable {
		// TODO Auto-generated method stub
		//保存采购订单
		stockOrderDaoProxy.addStockOrder(stockOrder);
		
		//保存过得采购订单id
		int stockOrderId = stockOrderDaoProxy.getStockOrderId(stockOrder.getStockCode()) ;
		//保存采购订单明细
		for (PStockOrderdetail stockOrderDetail : stockOrderDetails){
			stockOrderDetail.setStockOrderId(stockOrderId) ;
			stockOrderDetailDaoProxy.addStockOrderDetail(stockOrderDetail);			
		}
			
	}

	/**
	 * 删除采购订单及明细
	 */
	@Override
	public void deleteStockOrder(String stockOrderIds) {
		// TODO Auto-generated method stub

	}

	/**
	 * 修改采购订单及明细
	 */
	@Override
	public void editStockOrder(PStockOrder stockOrder,
			List<PStockOrderdetail> stockOrderDetails) throws Throwable {
		// TODO Auto-generated method stub

	}
	/**
	 * 动态查询采购订单
	 */
	@Override
	public List<StockOrderDTO> getStockOrdersByCondition(
			PStockOrder stockOrderModel, String startDate, String endDate)
			throws Throwable {
		// TODO Auto-generated method stub
		//采购订单DTO
		List<StockOrderDTO> stockOrderDTOs = new ArrayList<StockOrderDTO>() ;
		
		//采购订单
		List<PStockOrder> stockOrderModels = null ;
		if(stockOrderModel==null){
			stockOrderModels = stockOrderDaoProxy.getAllStockOrders();
		}
		else{
			stockOrderModels = stockOrderDaoProxy.getStockOrdersByCondition(stockOrderModel, startDate, endDate) ;
		}
		//封装dto
		if(stockOrderModels.size()>0){
			
			for(PStockOrder stockOrder:stockOrderModels){
				
				StockOrderDTO stockOrderDTO = new StockOrderDTO();
				stockOrderDTO.setStockOrder(stockOrder) ;
				
				//审核人
				PBaseVerifyprojectCandiate auditinger = null ;
				auditinger = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
				stockOrderDTO.setAuditinger(auditinger) ;
				
				//部门经理
				PBaseVerifyprojectCandiate manager = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
				stockOrderDTO.setManager(manager) ;
				
				//业务员
				PBaseVerifyprojectCandiate businesser = candiateDaoProxy.getCandiateById(stockOrder.getBusinesserId()) ;
				stockOrderDTO.setBusinesser(businesser) ;
				
				//结算方式
				PBaseSettlement settlement = settlementProxy.getSettlementById(stockOrder.getSettlementId()) ;
				stockOrderDTO.setSettlement(settlement) ;
				
				//部门
				PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(stockOrder.getDepartmentId()) ;
				stockOrderDTO.setDepartment(department) ;
				//币别
				PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(stockOrder.getCurrencyId()) ;
				stockOrderDTO.setCurrency(currency) ;
				
				//供应商
				PBaseVerifyprojectSupplier supplier = supplierDaoProxy.getSupplierById(stockOrder.getSupplierId()) ;
				stockOrderDTO.setSupplier(supplier) ;
			
				stockOrderDTOs.add(stockOrderDTO) ;
			}
			
		}
		return stockOrderDTOs;	
	}	
	/**
	 * 查询所有采购订单
	 * @return 返回采购订单DTO
	 */
	@Override
	public List<StockOrderDTO> getAllStockOrders() throws Throwable {
		// TODO Auto-generated method stub
		//采购订单DTO
		List<StockOrderDTO> stockOrderDTOs = new ArrayList<StockOrderDTO>() ;
		
		//采购订单
		List<PStockOrder> stockOrderModels = stockOrderDaoProxy.getAllStockOrders();
		
		//封装dto
		if(stockOrderModels.size()>0){
			
			for(PStockOrder stockOrder:stockOrderModels){
				
				StockOrderDTO stockOrderDTO = new StockOrderDTO();
				stockOrderDTO.setStockOrder(stockOrder) ;
				
				//审核人
				PBaseVerifyprojectCandiate auditinger = null ;
				auditinger = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
				stockOrderDTO.setAuditinger(auditinger) ;
				
				//部门经理
				PBaseVerifyprojectCandiate manager = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
				stockOrderDTO.setManager(manager) ;
				
				//业务员
				PBaseVerifyprojectCandiate businesser = candiateDaoProxy.getCandiateById(stockOrder.getBusinesserId()) ;
				stockOrderDTO.setBusinesser(businesser) ;
				
				//结算方式
				PBaseSettlement settlement = settlementProxy.getSettlementById(stockOrder.getSettlementId()) ;
				stockOrderDTO.setSettlement(settlement) ;
				
				//部门
				PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(stockOrder.getDepartmentId()) ;
				stockOrderDTO.setDepartment(department) ;
				//币别
				PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(stockOrder.getCurrencyId()) ;
				stockOrderDTO.setCurrency(currency) ;
				
				//供应商
				PBaseVerifyprojectSupplier supplier = supplierDaoProxy.getSupplierById(stockOrder.getSupplierId()) ;
				stockOrderDTO.setSupplier(supplier) ;
			
				stockOrderDTOs.add(stockOrderDTO) ;
			}
			
		}
		return stockOrderDTOs;
	}
	/**
	 * 查询采购订单及其明细信息，返回采购订单dto和采购明细dtolist
	 */
	@Override
	public Map<StockOrderDTO, List<StockOrderDetailDTO>> getStockOrderById(
			int stockOrderId) throws Throwable{
		// TODO Auto-generated method stub
		//用于封装采购订单dto和采购订单明细dto的map
		Map<StockOrderDTO, List<StockOrderDetailDTO>> map = new HashMap<StockOrderDTO, List<StockOrderDetailDTO>>();
		//采购订单model
		PStockOrder stockOrder = stockOrderDaoProxy.getStockOrderById(stockOrderId) ;
		
		//与采购订单关联的采购订单明细集合
		List<PStockOrderdetail>  stockOrderDetails =  new ArrayList<PStockOrderdetail>();
		List<StockOrderDetailDTO> stockOrderDetailDtos = new ArrayList<StockOrderDetailDTO>();
		
		//根据采购订单封装采购订单dto
		StockOrderDTO stockOrderDTO = new StockOrderDTO();
		stockOrderDTO.setStockOrder(stockOrder) ;		
		//审核人
		PBaseVerifyprojectCandiate auditinger = null ;
		auditinger = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
		stockOrderDTO.setAuditinger(auditinger) ;
		
		//部门经理
		PBaseVerifyprojectCandiate manager = candiateDaoProxy.getCandiateById(stockOrder.getAuditinger()) ;
		stockOrderDTO.setManager(manager) ;
		
		//业务员
		PBaseVerifyprojectCandiate businesser = candiateDaoProxy.getCandiateById(stockOrder.getBusinesserId()) ;
		stockOrderDTO.setBusinesser(businesser) ;
		
		//结算方式
		PBaseSettlement settlement = settlementProxy.getSettlementById(stockOrder.getSettlementId()) ;
		stockOrderDTO.setSettlement(settlement) ;
		
		//部门
		PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(stockOrder.getDepartmentId()) ;
		stockOrderDTO.setDepartment(department) ;
		//币别
		PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(stockOrder.getCurrencyId()) ;
		stockOrderDTO.setCurrency(currency) ;
		
		//供应商
		PBaseVerifyprojectSupplier supplier = supplierDaoProxy.getSupplierById(stockOrder.getSupplierId()) ;
		stockOrderDTO.setSupplier(supplier) ;
		
		
		//处理采购订单明细
		stockOrderDetails = stockOrderDetailDaoProxy.getStockOrderDetailsByStockOrderId(stockOrderId) ;
		//封装采购订单明细dtolist
		for(PStockOrderdetail stockOrderDetail:stockOrderDetails){
			
			StockOrderDetailDTO  stockOrderDetailDTO = new StockOrderDetailDTO();
			
			stockOrderDetailDTO.setStockOrderDetail(stockOrderDetail) ;
			
			PBaseMeasureunit measureUnit = measureUnitDaoProxy.getMeasureUnitById(stockOrderDetail.getMeasureunitId()) ;
			stockOrderDetailDTO.setMeasureUnit(measureUnit) ;
		
			PBaseVerifyprojectMaterial material = materialDaoProxy.getMaterialById(stockOrderDetail.getMaterialId()) ;
			stockOrderDetailDTO.setMaterial(material) ;
		
			stockOrderDetailDtos.add(stockOrderDetailDTO) ;
		}
		
		map.put(stockOrderDTO, stockOrderDetailDtos) ;
		
		return map;
		
	}
	
	/*setters and getters for instance variables */	

	public StockOrderDao getStockOrderDaoProxy() {
		return stockOrderDaoProxy;
	}

	public void setStockOrderDaoProxy(StockOrderDao stockOrderDaoProxy) {
		this.stockOrderDaoProxy = stockOrderDaoProxy;
	}

	public StockOrderDetailDao getStockOrderDetailDaoProxy() {
		return stockOrderDetailDaoProxy;
	}

	public void setStockOrderDetailDaoProxy(
			StockOrderDetailDao stockOrderDetailDaoProxy) {
		this.stockOrderDetailDaoProxy = stockOrderDetailDaoProxy;
	}

	public PBaseVerifyprojectCandiateDao getCandiateDaoProxy() {
		return candiateDaoProxy;
	}

	public void setCandiateDaoProxy(PBaseVerifyprojectCandiateDao candiateDaoProxy) {
		this.candiateDaoProxy = candiateDaoProxy;
	}

	public PBaseCurrencyDAO getCurrencyDaoProxy() {
		return currencyDaoProxy;
	}

	public void setCurrencyDaoProxy(PBaseCurrencyDAO currencyDaoProxy) {
		this.currencyDaoProxy = currencyDaoProxy;
	}

	public PBaseVerifyprojectDepartmentDao getDepartmentDaoProxy() {
		return departmentDaoProxy;
	}

	public void setDepartmentDaoProxy(
			PBaseVerifyprojectDepartmentDao departmentDaoProxy) {
		this.departmentDaoProxy = departmentDaoProxy;
	}

	public PBaseSettlementDAO getSettlementProxy() {
		return settlementProxy;
	}

	public void setSettlementProxy(PBaseSettlementDAO settlementProxy) {
		this.settlementProxy = settlementProxy;
	}

	public PBaseVerifyprogectSupplierDao getSupplierDaoProxy() {
		return supplierDaoProxy;
	}

	public void setSupplierDaoProxy(PBaseVerifyprogectSupplierDao supplierDaoProxy) {
		this.supplierDaoProxy = supplierDaoProxy;
	}

	public PBaseMeasureUnitDAO getMeasureUnitDaoProxy() {
		return measureUnitDaoProxy;
	}

	public void setMeasureUnitDaoProxy(PBaseMeasureUnitDAO measureUnitDaoProxy) {
		this.measureUnitDaoProxy = measureUnitDaoProxy;
	}

	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}

	public void setMaterialDaoProxy(PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}	
}
